﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Practices.Unity;
using Skynet.BusinessEntities;

namespace Skynet.BusinessLogic.Test.DataAccess
{
    [TestClass]
    public class ExamCategorySTETest
    {
        public static IUnityContainer Container;

        [ClassInitialize()]
        public static void Initialize(TestContext testContext)
        {

            Container = UnityHelper.BuildUnityContainer();
        }

        [TestMethod]
        public void OptimisticLockingExamCategorySTETest()
        {
            var ctx = Container.Resolve<BusinessEntities.SkyNetLibraryEntities>();
            var rep = Container.Resolve<BusinessLogic.DataAccess.IExamCategoryRepository>();
            var cat = new ExamCategory();
            cat.Id = Guid.NewGuid();
            cat.Title = "OptimisticCategory";
            cat.Description = "OptimisticCategory";
            cat.OptimisticId = Guid.NewGuid(); 
            rep.Maintenance(cat);

            var targetA = rep.GetOne(cat.Id);
            var targetB = rep.GetOne(cat.Id);

            targetA.StartTracking();
            targetB.StartTracking();


            targetA.OptimisticId = Guid.NewGuid();
            targetA.Title = "change";
            targetB.OptimisticId = Guid.NewGuid();
            targetB.Title = "change";

            rep.Maintenance(targetA);
            rep.Maintenance(targetB);

        }
        [TestMethod]
        public void MaintenanceExamCategorySTETest()
        {

            var ctx = Container.Resolve<BusinessEntities.SkyNetLibraryEntities>();
            var rep = Container.Resolve<BusinessLogic.DataAccess.IExamCategoryRepository>();

            var cat = new ExamCategory();
            cat.Id = Guid.NewGuid();
            cat.Title = "MaintenanceCategory";
            cat.Description = "MaintenanceCategory";            
            rep.Maintenance(cat);

            var target = rep.GetOne(cat.Id);


            

            Assert.IsNotNull(target);

            target.StartTracking();

            

            target.Title = "change";
            rep.Maintenance(target); 

            var ntarget = rep.GetOne(cat.Id);
            Assert.AreEqual<string>(target.Title, ntarget.Title);

            target.MarkAsDeleted();
            rep.Maintenance(target);
            ntarget = rep.GetOne(cat.Id);

            Assert.IsNull(ntarget);
            





 
        }
        
    }
}
